﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CountryActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The country actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Locations
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Commands;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The country actioner.
    /// </summary>
    public class CountryActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CountryActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public CountryActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create country.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateCountry(string name)
        {
            var where = new Country();
            where.ID = Guid.NewGuid();
            where.CreatedDateTime = DateTime.Now;
            where.Name = name;
            this.Session.Save(where);

            this.Log.CreateLogEntry(
                where.ID, string.Format("Land '{0}', blev oprettet.", where.Name), Enums.LogEntryType.Country);

            return where.ID;
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="CountryResult"/>.
        /// </returns>
        public CountryResult ExecuteSearchQuery(CountryQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new CountryResult();

            IQueryOver<Country, Country> search = this.Session.QueryOver<Country>();
            this.ApplyDefaultCriterias(settings, search);
            result.TotalRowCount = search.RowCount();

            var countries = ApplySkipAndTake(settings, search.OrderBy(x => x.Name).Asc);
            foreach (Country country in countries)
            {
                Services.Dto.Country dto = Services.Dto.Country.Convert(country);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get country.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="Country"/>.
        /// </returns>
        public Country GetCountry(Guid id)
        {
            var where = this.Session.Get<Country>(id);

            return where;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The create country.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void CreateCountry(CreateCountryCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var where = new Country { ID = cmd.Dto.ID, Name = cmd.Dto.Name };

                this.Session.Save(where);
                this.Log.CreateLogEntry(
                    where.ID, 
                    string.Format("Land '{0}', blev oprettet.", where.Name),
                    Enums.LogEntryType.Country,
                    Enums.LogEntryActionType.Create);
            }
        }

        /// <summary>
        /// The delete country.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void DeleteCountry(DeleteCountryCommand cmd)
        {
            var country = this.Session.Get<Country>(cmd.Dto);

            if (country != null)
            {
                int countryUsage = this.Session.QueryOver<Region>().Where(x => x.Country.ID == country.ID).RowCount();
                if (countryUsage == 0)
                {
                    this.Session.Delete(country);
                }
            }
        }

        /// <summary>
        /// The update country.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void UpdateCountry(UpdateCountryCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var where = this.Session.Get<Country>(cmd.Dto.ID);
                if (where != null)
                {
                    where.Name = cmd.Dto.Name;
                    this.Session.Update(where);
                }
            }
        }

        #endregion
    }
}